package com.smile.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.GetInspectionTaskByFlightIdRequestDTO;
import com.smile.model.dto.request.GetInspectionTaskDetailRequestDTO;
import com.smile.model.dto.request.GetInspectionTaskListRequestDTO;
import com.smile.model.request.GetInspectionTaskByFlightIdRequest;
import com.smile.model.response.GetInspectionTaskByFlightIdResponse;
import com.smile.model.response.response.GetInspectionTaskByFlightIdResponseDTO;
import com.smile.model.response.response.GetInspectionTaskDetailResponseDTO;
import com.smile.model.response.response.GetInspectionTaskListResponseDTO;
import com.smile.model.request.GetInspectionTaskDetailRequest;
import com.smile.model.request.GetInspectionTaskListRequest;
import com.smile.model.response.GetInspectionTaskDetailResponse;
import com.smile.model.response.GetInspectionTaskListResponse;
import com.smile.service.InspectionTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("inspectionTask")
@RestController
public class InspectionTaskController {

    @Autowired
    private InspectionTaskService inspectionTaskService;

    @PostMapping("/getInspectionTaskList")
    public R<Page<GetInspectionTaskListResponse>> getInspectionTaskList(@RequestBody GetInspectionTaskListRequest getInspectionTaskListRequest) {
        GetInspectionTaskListRequestDTO getInspectionTaskListRequestDTO = BeanUtil.copyProperties(getInspectionTaskListRequest, GetInspectionTaskListRequestDTO.class);
        Page<GetInspectionTaskListResponseDTO> page = inspectionTaskService.getInspectionTaskList(getInspectionTaskListRequestDTO);
        return R.ok(MyBatisPlusPageConverter.convertPage(page, GetInspectionTaskListResponse.class));
    }

    @PostMapping("/getInspectionTaskDetail")
    public R<GetInspectionTaskDetailResponse> getInspectionTaskDetail(@RequestBody GetInspectionTaskDetailRequest getInspectionTaskDetailRequest) {
        GetInspectionTaskDetailRequestDTO getInspectionTaskListRequestDTO = BeanUtil.copyProperties(getInspectionTaskDetailRequest, GetInspectionTaskDetailRequestDTO.class);
        GetInspectionTaskDetailResponseDTO getInspectionTaskDetailResponseDTO = inspectionTaskService.getInspectionTaskDetail(getInspectionTaskListRequestDTO);
        return R.ok(BeanUtil.copyProperties(getInspectionTaskDetailResponseDTO, GetInspectionTaskDetailResponse.class));
    }
    @PostMapping("/cancelExecute")
    public R<Void> cancelExecute(@RequestBody GetInspectionTaskDetailRequest getInspectionTaskDetailRequest) {
        GetInspectionTaskDetailRequestDTO getInspectionTaskListRequestDTO = BeanUtil.copyProperties(getInspectionTaskDetailRequest, GetInspectionTaskDetailRequestDTO.class);
        Boolean result = inspectionTaskService.cancelExecute(getInspectionTaskListRequestDTO);
        if (result) {
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/stopExecute")
    public R<Void> stopExecute(@RequestBody GetInspectionTaskDetailRequest getInspectionTaskDetailRequest) {
        GetInspectionTaskDetailRequestDTO getInspectionTaskListRequestDTO = BeanUtil.copyProperties(getInspectionTaskDetailRequest, GetInspectionTaskDetailRequestDTO.class);
        // 使用cancelExecute方法，因为stopExecute在服务接口中不存在
        Boolean result = inspectionTaskService.cancelExecute(getInspectionTaskListRequestDTO);
        if (result) {
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/getInspectionTaskByFlightId")
    @SaIgnore
    public R<GetInspectionTaskByFlightIdResponse> getInspectionTaskByFlightId(@RequestBody GetInspectionTaskByFlightIdRequest getInspectionTaskByFlightIdRequest) {
        GetInspectionTaskByFlightIdRequestDTO getInspectionTaskByFlightIdRequestDTO = BeanUtil.copyProperties(getInspectionTaskByFlightIdRequest, GetInspectionTaskByFlightIdRequestDTO.class);
        GetInspectionTaskByFlightIdResponseDTO inspectionTaskByFlightId = inspectionTaskService.getInspectionTaskByFlightId(getInspectionTaskByFlightIdRequestDTO);
        return R.ok(BeanUtil.copyProperties(inspectionTaskByFlightId, GetInspectionTaskByFlightIdResponse.class));
    }

    @GetMapping("/updateTaskStatus")
    public R<Void> updateTaskStatus(@RequestParam("organizationId") Long organizationId,@RequestParam(value = "status", defaultValue = "3") Integer status) {
        inspectionTaskService.updateTaskStatus(organizationId, status);
        return R.ok();
    }
}
